function resizeDIV(idDIV) {
     
    var width = $(window).width();        
    var height = $(window).height();
    document.getElementById(idDIV).width(width/2);
    document.getElementById(idDIV).height(height);
                /*var width = $(window).width(); 
                var height = $(window).height();
                $("#listeBE").width(width/2);
                $("#listeBE").height(height);
                $("#BEAnnee").width(width/2);
                $("#BEAnnee").height(height); */
};

function infoSeances(){
    var data=[];
    var checkboxValues = [];
    var uncheckboxValues = [];
    $('input[type="checkbox"]').each(function(index, elem) {
        var seance=[];
        if ($(elem).prop('checked') == false){        
        
            seance.push($(elem).val());              
            seance.push($("label[id='location"+$(elem).attr("id")+"']").text());            
            seance.push($("label[id='date"+$(elem).attr("id")+"']").text());          
            uncheckboxValues.push(seance);           
        }           
        else {           
            seance.push($(elem).val());              
            seance.push($("label[id='location"+$(elem).attr("id")+"']").text());            
            seance.push($("label[id='date"+$(elem).attr("id")+"']").text());          
            checkboxValues.push(seance);        
        }  
                    
    });
    if ((typeof checkboxValues != "undefined") && (checkboxValues != null) && (checkboxValues.length > 0)){
        data.push(checkboxValues);
    }
    else{
        data.push("no check");
    }
    
    if ((typeof uncheckboxValues != "undefined") && (uncheckboxValues != null) && (uncheckboxValues.length > 0)){
        data.push(uncheckboxValues);
    }
    else{
        data.push("no uncheck");
    }
    return data;
}

$(document).ready(function(){
                var width = $(window).width(); 
                var height = $(window).height();
                $("#listeBE").width(width/3);
                $("#listeBE").height(height);
                $("#BEAnnee").width(width/3);
                $("#BEAnnee").height(height);
                
                //Ajouter les BEs à partir du fichier Ical
                $("#ajaxURLForm").submit(function(){
                    console.log("URL inserted");
                    $.ajax({
                      type: "POST",
                      url: "../controller/URL.php",
                      data:{
                          req: "URL",
                          data:$("#URL").val(),
                          type:$("input[name='typeCategorie']:checked").val()
                          //user: $("#ajaxEmail").val(),
                          //pass: $("#ajaxPassword").val()
                      },
                      success: function (data){
                          var json = jQuery.parseJSON(data);
                          if (json.success){
                              console.log("OK URL");
                              //$("div#listeBE").append("<h2>Well done</h2><br/>");
                              //$("div#listeBE").append(json.msg);

                              //$("#BE").append(json.msg);
                              //alert(json.msg);
                              $("#BE").html(json.msg);
                              //$("#ajaxGroupBEForm").appendTo( ".ui-page" ).trigger( "create" );
                              //$("#interfaceAdmin").appendTo( ".ui-page" ).trigger( "create" );
                              $("#gestionBEs").trigger( "create" );
                              $("#URL").val("");
                              
                          }else {
                              console.log("Pb avec URL");
                          }
                          
                          
                      }
                   });
                   
                    return false;
                });
                
                //Ajouter BE
                $("#listeBE").on("click",'#submitSelectBE', function(e){
                    console.log("clicked");               
                    if ($("#listeSeance").length){
                        var id= $("div:jqmData(role='collapsible')").length + 1;
                        var value = infoSeances();
                        
                        if (value[0] != "no check"){

                            value.push(id);
                        
                            $.ajax({
                              type: "POST",
                              url: "../controller/URL.php",
                              data:{
                                  req: "newProjet",
                                  data: value
                              },
                              success: function (data){
                                  var json = jQuery.parseJSON(data);
                                  if (json.success){
                                      console.log("OK projet");
                                      $("#BE").html(json.msg);
                                      $("#projects").append(json.msg2);
                                      //$("#ajaxGroupBE Form").appendTo( ".ui-page" ).trigger( "create" );
                                      //$("#interfaceAdmin").appendTo( ".ui-page" ).trigger( "create" );
                                      $("#gestionBEs").trigger( "create" );
                                  }else {
                                      //alert("PB2");
                                      console.log(json.msg);
                                  }
                              }
                           });
                        }
                        else {
                            $("#popupAlertRegroup2").popup("open");
                            if ( $("#popupAlertRegroup").parent().hasClass('ui-popup-hidden')) {
                                alert(
                                    "Error: Popup should not be visible."
                                );
                                return( -1 );
                            }
                        }
                        //value.push($("#groupe option:selected").text());
                        
                    }
                    else{
                        console.log("No URL or No BE");
                        $("#popupAlertRegroup1").popup("open");
                        if ( $("#popupAlertRegroup").parent().hasClass('ui-popup-hidden')) {
                            alert(
                                "Error: Popup should not be visible."
                            );
                            return( -1 );
                        }
                    }
                   return false;
                });
                
                //Supprimer une seance dans un BE choisie
                $("#projects").on("click",'button[name="seanceButton"]',function(){
                    /*var idSeance= $(this).attr("id");              
                    $("#popupConfirmDel").popup("open");
                    if ( $("#popupConfirmDel").parent().hasClass('ui-popup-hidden')) {
                        alert(
                            "Error: Popup should not be visible."
                        );
                        return( -1 );
                    }
                    else{
                        $("#popupConfirmDel").on("click","#confirmButton",function(){   
                            if ($(this).text() == "Yes"){
                                console.log("YES CONFIRM");
                                
                                $("#projects").find("button[id='"+idSeance+"']").remove();                        
                                $.ajax({
                                    type: "POST",
                                    url: "../controller/URL.php",
                                    data:{
                                        req: "modifyProjet",
                                        data: idSeance
                                    },
                                    success: function (data){
                                        var json = jQuery.parseJSON(data);
                                        if (json.success){
                                            console.log("OK modify");
                                            $('fieldset[id="listeSeance"]').find(".ui-controlgroup-controls").append(json.msg);
                                            $("#gestionBEs").trigger( "create" );
                                        }else {
                                            console.log(json.msg);
                                        }
                                    }
                                 });
                            }
                            else{
                                console.log("NO CONFIRM");
                            }
                        });
                    }*/          
                    
                    if (confirm("Voulez vous vraiment supprimer cette seance?") == true) {
                        $(this).remove();

                        $.ajax({
                                type: "POST",
                                url: "../controller/URL.php",
                                data:{
                                    req: "modifyProjet",
                                    data: $(this).attr("id")
                                },
                                success: function (data){
                                    var json = jQuery.parseJSON(data);
                                    if (json.success){
                                        console.log("OK modify");
                                        $('fieldset[id="listeSeance"]').find(".ui-controlgroup-controls").append(json.msg);
                                        $("#gestionBEs").trigger( "create" );
                                    }else {
                                        console.log(json.msg);
                                    }
                                }
                             });
                    } else {
                        console.log("KEEP SEANCE");
                    }
                    return false;
                });
                
                //Supprimer un BE choisie
                $("#projects").on("click",'a[name="supprimerBE"]',function(){

                    var par=$(this).attr("id").split("-");
                    
                    if (confirm("Voulez vous vraiment supprimer ce Projet?") == true) {
                                     
                        var info=[];
                                     
                        //collect seances
                        $("#BE" + par[2]).find('button[name="seanceButton"]').each(function(index, elem){
                            info.push($(elem).attr("id"));
                        });
                        
                        //return Seances
                        $.ajax({
                                type: "POST",
                                url: "../controller/URL.php",
                                data:{
                                    req: "deleteProjet",
                                    data: info
                                },
                                success: function (data){
                                    var json = jQuery.parseJSON(data);
                                    if (json.success){
                                        console.log("OK delete projet");
                                        $('fieldset[id="listeSeance"]').find(".ui-controlgroup-controls").append(json.msg);
                                        $("#gestionBEs").trigger( "create" );
                                    }else {
                                        console.log(json.msg);
                                    }
                                }
                             });

                        var idSearch=par[1]+par[2];
                        //remove BE
                        $("#projects").find("div:jqmData(role='collapsible')").each(function(index, elem){
                            if ($(elem).attr("id") == idSearch){
                                $(elem).remove();
                            }
                        });
                        console.log("Everything OK. Projet is deleted");
                    } else {
                        console.log("NON, DON'T DELETE PROJET");
                    }
                    return false;
                });
                
                //Valide BEs choisies
                $("#valideButton1").on("click",function(){
                    console.log("valideProjet");
                    var totalData=[];
                    var totalBEinfo=[];
                    if ($("#projects").find("div:jqmData(role='collapsible')").length){
                        //BE informations 
                        $("#projects").find("input[type='text']").each(function(index, elem){

                            var BEinformation=[];
                            var nameBE=$(elem).attr("id").split("-");
                            BEinformation.push($(elem).val());

                            //Ajouter seances informations
                            var seance=[];
                            $("#"+nameBE[1]).find('button[name="seanceButton"]').each(function(index, elem){
                                //var infoSean=$(elem).attr("id").split(";");

                                seance.push($(elem).attr("id"));
                            });

                            BEinformation.push(seance);

                            totalBEinfo.push(BEinformation);

                        });

                        //Classe
                        totalData.push($("#groupe option:selected").text());
                        //BE info
                        totalData.push(totalBEinfo);
                        console.log("send info");
                        $.ajax({
                              type: "POST",
                              url: "../controller/URL.php",
                              data:{
                                  req: "valideProjet",
                                  data: totalData
                              },
                              success: function (data){
                                  var json = jQuery.parseJSON(data);
                                  if (json.success){
                                      console.log("BDD OK");

                                      //remove BE
                                      $("#projects").find("div:jqmData(role='collapsible')").each(function(index, elem){
                                          $(elem).remove();
                                      });
                                      console.log(json.msg);
                                  }else {
                                      //alert("PB2");
                                      console.log("Probleme avec BDD");
                                  }
                              }
                           });
                    }
                    else{
                        console.log("No BE à valider");
                        $("#popupAlertValidate").popup("open");

                        if ( $("#popupAlertValidate").parent().hasClass('ui-popup-hidden')) {
                            alert(
                                "Error: Popup should not be visible."
                            );
                            return( -1 );
                        }
                    }
                    
                    return false;
                });
                
                //Valide Examens choisies
                $("#valideButton2").on("click",function(){
                    console.log("valideExamen");
                    var totalData=[];
                    var totalExamInfo=[];
                    if ($("#projects").find("div:jqmData(role='collapsible')").length){
                        //Examen informations 
                        $("#projects").find("input[type='text']").each(function(index, elem){

                            var ExamInfo=[];
                            var nameExamen=$(elem).attr("id").split("-");
                            ExamInfo.push($(elem).val());

                            //Ajouter seances informations
                            var seance= $("#"+nameExamen[1]).find('button[name="seanceButton"]').attr("id");
                            /*$("#"+nameExamen[1]).find('button[name="seanceButton"]').each(function(index, elem){
                                //var infoSean=$(elem).attr("id").split(";");

                                seance.push($(elem).attr("id"));
                            });*/
                            alert("seance");

                            ExamInfo.push(seance);

                            totalExamInfo.push(ExamInfo);

                        });

                        //Classe
                        totalData.push($("#groupe option:selected").text());
                        //BE info
                        totalData.push(totalExamInfo);
                        console.log("send info");
                        $.ajax({
                              type: "POST",
                              url: "../controller/URL.php",
                              data:{
                                  req: "valideExamen",
                                  data: totalData
                              },
                              success: function (data){
                                  var json = jQuery.parseJSON(data);
                                  if (json.success){
                                      console.log("BDD OK");

                                      //remove BE
                                      $("#projects").find("div:jqmData(role='collapsible')").each(function(index, elem){
                                          $(elem).remove();
                                      });
                                      console.log(json.msg);
                                  }else {
                                      //alert("PB2");
                                      console.log("Probleme avec BDD");
                                  }
                              }
                           });
                    }
                    else{
                        console.log("No Examen à valider");
                        $("#popupAlertValidate").popup("open");

                        if ( $("#popupAlertValidate").parent().hasClass('ui-popup-hidden')) {
                            alert(
                                "Error: Popup should not be visible."
                            );
                            return( -1 );
                        }
                    }
                    
                    return false;
                });

});
